//
//  ZGWSelectProviceCityDiQuViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/6/2.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ZGWSelectProviceCityDiQuViewController.h"

@interface ZGWSelectProviceCityDiQuViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property(nonatomic,strong)NSDictionary *pickerDic;
@property(nonatomic,strong)NSArray *provinceArray;
@property(nonatomic,strong)NSArray *selectedArray;
@property(nonatomic,strong)NSArray *cityArray;
@property(nonatomic,strong)NSArray *townArray;
@property(nonatomic,strong)UIPickerView *pickView;//地址选择视图

@property (nonatomic, strong) UILabel *resultDiQuLabel; // 地区的展示结果
@property (nonatomic, strong) UIButton *noLimitButton; // 不限按钮


@end

@implementation ZGWSelectProviceCityDiQuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加地区数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"选择地区";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    UIBarButtonItem *fanHuiBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
   
    self.navigationItem.leftBarButtonItem = fanHuiBarItem;
    
    // 右方
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeSystem];
    finishButton.frame = CGRectMake(0, 0, 50, 23);
    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(finishSelectDiQu:) forControlEvents:UIControlEventTouchUpInside];
    finishButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k16Big];
    finishButton.layer.borderWidth = 1 * kAdjustParamater;
    finishButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    finishButton.layer.cornerRadius = 6 * kAdjustParamater;
    finishButton.layer.masksToBounds = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:finishButton];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 完成选择地区
- (void)finishSelectDiQu:(UIButton *)button
{
    NSLog(@"完成地区选择");
//    NSString *province = [self.provinceArray objectAtIndex:[_pickView selectedRowInComponent:0]];
//    NSString *city = [self.cityArray objectAtIndex:[_pickView selectedRowInComponent:1]];
//    NSString *town = [self.townArray objectAtIndex:[_pickView selectedRowInComponent:2]];
//    NSLog(@"省:%@,市:%@,区:%@", province, city, town);
    NSLog(@"选择结果:%@", self.resultDiQuLabel.text);
    self.selectDiQu(self.resultDiQuLabel.text);
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 建立数据源
- (void)addData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.provinceArray = [self.pickerDic allKeys];
    self.selectedArray = [self.pickerDic objectForKey:[[self.pickerDic allKeys] objectAtIndex:0]];
    
    if (self.selectedArray.count > 0) {
        self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    
    if (self.cityArray.count > 0) {
        self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
    }
}

#pragma mark - 布局
- (void)addMySubview
{
    _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 120 * kAdjustParamater, kScreenWidth, 300 * kAdjustParamater)];
    _pickView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    _pickView.dataSource = self;
    _pickView.delegate =self;
    //  [_bottomView addSubview:_pickView];
    
    [self.view addSubview:_pickView];
    
    // 地区的展示结果
    self.resultDiQuLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 30 * kAdjustParamater, 345 * kAdjustParamater, 30 * kAdjustParamater)];
    self.resultDiQuLabel.backgroundColor = [UIColor whiteColor];
    self.resultDiQuLabel.text = @"湖南省-湘潭市-雨湖区";
    self.resultDiQuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.resultDiQuLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k16Big];
    self.resultDiQuLabel.textAlignment = NSTextAlignmentCenter;
    self.resultDiQuLabel.layer.cornerRadius = 4 * kAdjustParamater;
    self.resultDiQuLabel.layer.borderColor = [kCyanColor CGColor];
    self.resultDiQuLabel.layer.borderWidth = 2 * kAdjustParamater;
    [self.view addSubview:_resultDiQuLabel];
    
    // 不限按钮
    self.noLimitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.noLimitButton.frame = CGRectMake(310 * kAdjustParamater, self.resultDiQuLabel.bottom + 20 * kAdjustParamater, 50 * kAdjustParamater, 23 * kAdjustParamater);
    [self.noLimitButton setTitle:@"不限" forState:UIControlStateNormal];
    [self.noLimitButton setTitleColor:kCyanColor forState:UIControlStateNormal];
    [self.noLimitButton addTarget:self action:@selector(noLimit:) forControlEvents:UIControlEventTouchUpInside];
    self.noLimitButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:k16Big];
    self.noLimitButton.layer.borderWidth = 1 * kAdjustParamater;
    self.noLimitButton.layer.borderColor = [kCyanColor CGColor];
    self.noLimitButton.layer.cornerRadius = 6 * kAdjustParamater;
    self.noLimitButton.layer.masksToBounds = YES;
    [self.view addSubview:_noLimitButton];
    
}

#pragma mark - 不限
- (void)noLimit:(UIButton *)button
{
    NSLog(@"不限");
    self.resultDiQuLabel.text = @"不限";
}

#pragma mark - UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _provinceArray.count;
    } else if (component == 1) {
        return _cityArray.count;
    } else {
        return _townArray.count;
    }
}


-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lable=[[UILabel alloc]init];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[UIFont systemFontOfSize:16.0f];
    if (component == 0) {
        lable.text=[self.provinceArray objectAtIndex:row];
    } else if (component == 1) {
        lable.text=[self.cityArray objectAtIndex:row];
    } else {
        lable.text=[self.townArray objectAtIndex:row];
    }
    return lable;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 90 * kAdjustParamater;
    } else if (component == 1 || component == 2) {
        return 285 / 2 * kAdjustParamater;
    }
    return 0;
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:row]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        } else {
            self.cityArray = nil;
        }
        if (self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        } else {
            self.townArray = nil;
        }
    }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:row]];
        } else {
            self.townArray = nil;
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    
    [pickerView reloadComponent:2];
    
    NSString *province = [self.provinceArray objectAtIndex:[_pickView selectedRowInComponent:0]];
    NSString *city = [self.cityArray objectAtIndex:[_pickView selectedRowInComponent:1]];
    NSString *town = [self.townArray objectAtIndex:[_pickView selectedRowInComponent:2]];
  //  NSLog(@"省:%@,市:%@,区:%@", province, city, town);
    self.resultDiQuLabel.text = [NSString stringWithFormat:@"%@-%@-%@", province, city, town];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
